package aparicio.mbox.parse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class CountItemsList<E> extends ArrayList<E> {

	private Map<E, Integer> count = new HashMap<E, Integer>();
	
	public boolean add(E element) {
		if (!count.containsKey(element)) {
			count.put(element, 1);
		} else {
			count.put(element, count.get(element) + 1);
		}
		
		return super.add(element);
	}
	
	/**Gets the count of how many times the same thing occurs in the list.
	 * 
	 * @param element The thing that occurs in the list.
	 * 
	 * @return The number of times the thing occurs in the list
	 */
	public int getCount(E element) {
		if (!count.containsKey(element))
			return 0;
		
		return count.get(element);
	}
	
	public int size() {
		return count.size();
	}
	
	public String toString() {
		StringBuilder str = new StringBuilder();
		
		for(E elem: count.keySet()) {
			str.append(elem + ": Count=" + getCount(elem) + ", ");
		}
		
		if(str.length() > 0){
			str.deleteCharAt(str.length()-1);
			str.deleteCharAt(str.length()-1);
		}
		
		return str.toString();
	}
}
